
global user "`c(username)'"
global dirdata "C:\Users\\$user\Dropbox\CCT BJP\data"
global dir1  "$dirdata\Final data"
global dir2  "$dirdata\Results\graphs"
global dir3  "$dirdata\Results\"
cd "$dir3"

use "$dir1\pooled_hh.dta", clear
keep if tau>-5  
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1

g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=w_hoursw_head>0
g informal4_head=(informal4_m==1 | informal4_p==1)
replace informal4_head=. if work_p==. & work_m==.

g wageemp4_m=(work_m==1 & informal4_m==0)
replace wageemp4_m=. if work_m==.
g wageemp4_p=(work_p==1 & informal4_p==0)
replace wageemp4_p=. if work_p==.
g wageemp4_head=(wageemp4_m==1 | wageemp4_p==1)
replace wageemp4_head=. if work_m==. & work_p==.

egen w_ylabor_head=rowtotal(w_ylabor_m w_ylabor_p)
egen w_rev_head=rowtotal(w_rev_m w_rev_p)
egen w_insumos_head=rowtotal(w_insumos_p w_insumos_m)
egen w_salarios_head=rowtotal(w_salarios_p w_salarios_m)
egen w_costs_t=rowtotal(w_insumos_t w_salarios_t)
egen w_costs_head=rowtotal(w_insumos_head w_salarios_head)
egen w_costs_m=rowtotal(w_insumos_m w_salarios_m)
egen w_costs_p=rowtotal(w_insumos_p w_salarios_p)
replace w_costs_t=. if year==2002
replace w_costs_head=. if year==2002
replace w_costs_p=. if year==2002
replace w_costs_m=. if year==2002

g w_pro_t=w_rev_t-w_costs_t
g w_pro_head=w_rev_head-w_costs_head
g w_pro_m=w_rev_m-w_costs_m
g w_pro_p=w_rev_p-w_costs_p

g any_costs_head=w_costs_head>0
g any_costs_p=w_costs_p>0
g any_costs_m=w_costs_m>0
replace any_costs_head=. if w_costs_head==.
replace any_costs_p=. if  w_costs_p==.
replace any_costs_m=. if w_costs_m==.
local sample1 "(min_schooling>=1 & min_schooling<9) "

local parm Post



global covs4 "age spanish schooling hhmale  urban np nchild5  hhmale age_sac  c.age_sac#i.year c.np#i.year   i.departamento#i.year i.year"
local spec4  Post i.min_schooling  $covs4  
local replace replace


label var informal4_head "Self-employed"
label var informal4_m "Self-employed"
label var informal4_p "Self-employed"
label var wageemp4_head "Employee"
label var wageemp4_m "Employee"
label var wageemp4_p "Employee"

foreach x in informal4_head wageemp4_head w_rev_head w_costs_head   any_costs_head {
sum `x'  [w=factor] if tau==-1 & `sample1'
local mean_dv=r(mean)
areg `x'  `spec4' [w=factor] if `sample1'  , a(departamento) cluster(municipio)
outreg2 using "$dir3\Table5", excel `replace' label keep(`parm') less(0) nocons  bdec(3) adds(Clusters, e(N_clust),Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES, Region X year FE, YES )   
local replace append
}


global covs4 "age_m spanish_m  schooling_m age spanish schooling hhmale  urban np nchild5  hhmale age_sac  c.age_sac#i.year c.np#i.year   i.departamento#i.year i.year"
local spec4  Post i.min_schooling  $covs4  
foreach x in  informal4_m wageemp4_m w_rev_m w_costs_m any_costs_m {
sum `x'  [w=factor] if tau==-1 & `sample1'
local mean_dv=r(mean)
areg `x'  `spec4' [w=factor]  if `sample1'  , a(departamento) cluster(municipio)
outreg2 using "$dir3\Table5", excel `replace' label keep(`parm') less(0) nocons  bdec(3) adds(Clusters, e(N_clust),Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES, Region X year FE, YES )   
local replace append
}



global covs4 "age_p spanish_p  schooling_p age spanish schooling hhmale  urban np nchild5  hhmale age_sac  c.age_sac#i.year c.np#i.year   i.departamento#i.year i.year"
local spec4  Post i.min_schooling  $covs4  
foreach x in  informal4_p wageemp4_p w_rev_p w_costs_p w_insumos_p w_salarios_p any_costs_p {
sum `x'  [w=factor]  if tau==-1 & `sample1'
local mean_dv=r(mean)
areg `x'  `spec4' [w=factor] if `sample1'  , a(departamento) cluster(municipio)
outreg2 using "$dir3\Table5", excel `replace' label keep(`parm') less(0) nocons  bdec(3) adds(Clusters, e(N_clust),Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES, Region X year FE, YES )   
local replace append
}

